<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/enum.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../high_level_components.html" title="Chapter 2. High Level Components">
<link rel="prev" href="boost_python_docstring_options_h.html" title="boost/python/docstring_options.hpp">
<link rel="next" href="boost_python_errors_hpp.html" title="boost/python/errors.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_docstring_options_h.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_errors_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="high_level_components.boost_python_enum_hpp"></a><a class="link" href="boost_python_enum_hpp.html" title="boost/python/enum.hpp">boost/python/enum.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum">Class
        template <code class="computeroutput"><span class="identifier">enum_</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum_constructors">Class
        template <code class="computeroutput"><span class="identifier">enum_</span></code> constructors</a></span></dt>
<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum_modifier_fun">Class
        template <code class="computeroutput"><span class="identifier">enum_</span></code> modifier functions</a></span></dt>
<dt><span class="section"><a href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_enum_hpp.introduction"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          &lt;boost/python/enum.hpp&gt; defines the interface through which users
          expose their C++ enumeration types to Python. It declares the <code class="computeroutput"><span class="identifier">enum_</span></code> class template, which is parameterized
          on the enumeration type being exposed.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_enum_hpp.class_template_enum"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum" title="Class template enum_">Class
        template <code class="computeroutput"><span class="identifier">enum_</span></code></a>
</h3></div></div></div>
<p>
          Creates a Python class derived from Python's <code class="computeroutput"><span class="keyword">int</span></code>
          type which is associated with the C++ type passed as its first parameter.
        </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
  <span class="keyword">class</span> <span class="identifier">enum_</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">object</span>
  <span class="special">{</span>
    <span class="identifier">enum_</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span> <span class="special">=</span> <span class="number">0</span><span class="special">);</span>
    <span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&amp;</span> <span class="identifier">value</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">T</span><span class="special">);</span>
    <span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&amp;</span> <span class="identifier">export_values</span><span class="special">();</span>
  <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_enum_hpp.class_template_enum_constructors"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum_constructors" title="Class template enum_ constructors">Class
        template <code class="computeroutput"><span class="identifier">enum_</span></code> constructors</a>
</h3></div></div></div>
<pre class="programlisting"><span class="identifier">enum_</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">doc</span><span class="special">=</span><span class="number">0</span><span class="special">);</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                name is an <a class="link" href="../glossary.html#ntbs">ntbs</a> which conforms to Python's
                <a href="http://www.python.org/doc/current/ref/identifiers.html" target="_top">identifier
                naming rules</a>.
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd><p>
                Constructs an <code class="computeroutput"><span class="identifier">enum_</span></code>
                object holding a Python extension type derived from <code class="computeroutput"><span class="keyword">int</span></code> which is named <code class="computeroutput"><span class="identifier">name</span></code>.
                The named attribute of the <a class="link" href="boost_python_scope_hpp.html" title="boost/python/scope.hpp">current
                scope</a> is bound to the new extension type.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_enum_hpp.class_template_enum_modifier_fun"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.class_template_enum_modifier_fun" title="Class template enum_ modifier functions">Class
        template <code class="computeroutput"><span class="identifier">enum_</span></code> modifier functions</a>
</h3></div></div></div>
<pre class="programlisting"><span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&amp;</span> <span class="identifier">value</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                name is an <a class="link" href="../glossary.html#ntbs">ntbs</a> which conforms to Python's
                <a href="http://www.python.org/doc/current/ref/identifiers.html" target="_top">identifier
                naming rules</a>.
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd><p>
                adds an instance of the wrapped enumeration type with value x to
                the type's dictionary as the named attribute.
              </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
              </p></dd>
</dl>
</div>
<pre class="programlisting"><span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&amp;</span> <span class="identifier">export_values</span><span class="special">();</span></pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Effects</span></dt>
<dd><p>
                sets attributes in the <a class="link" href="boost_python_scope_hpp.html" title="boost/python/scope.hpp">current
                scope</a> with the same names and values as all enumeration values
                exposed so far by calling value().
              </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                <code class="computeroutput"><span class="special">*</span><span class="keyword">this</span></code>
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="high_level_components.boost_python_enum_hpp.example"></a><a class="link" href="boost_python_enum_hpp.html#high_level_components.boost_python_enum_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          C++ module definition
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">enum</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">def</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>

<span class="keyword">enum</span> <span class="identifier">color</span> <span class="special">{</span> <span class="identifier">red</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">green</span> <span class="special">=</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">blue</span> <span class="special">=</span> <span class="number">4</span> <span class="special">};</span>

<span class="identifier">color</span> <span class="identifier">identity_</span><span class="special">(</span><span class="identifier">color</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span> <span class="special">}</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">enums</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">color</span><span class="special">&gt;(</span><span class="string">"color"</span><span class="special">)</span>
    <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"red"</span><span class="special">,</span> <span class="identifier">red</span><span class="special">)</span>
    <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"green"</span><span class="special">,</span> <span class="identifier">green</span><span class="special">)</span>
    <span class="special">.</span><span class="identifier">export_values</span><span class="special">()</span>
    <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"blue"</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">)</span>
    <span class="special">;</span>

  <span class="identifier">def</span><span class="special">(</span><span class="string">"identity"</span><span class="special">,</span> <span class="identifier">identity_</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
          Interactive Python:
        </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">from</span> <span class="identifier">enums</span> <span class="identifier">import</span> <span class="special">*</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">red</span><span class="special">)</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">red</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">red</span><span class="special">)</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">red</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">green</span><span class="special">)</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">green</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">green</span><span class="special">)</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">green</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">blue</span><span class="special">)</span>
<span class="identifier">Traceback</span> <span class="special">(</span><span class="identifier">most</span> <span class="identifier">recent</span> <span class="identifier">call</span> <span class="identifier">last</span><span class="special">):</span>
  <span class="identifier">File</span> <span class="string">"&lt;stdin&gt;"</span><span class="special">,</span> <span class="identifier">line</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">in</span> <span class="special">?</span>
<span class="identifier">NameError</span><span class="special">:</span> <span class="identifier">name</span> <span class="char">'blue'</span> <span class="identifier">is</span> <span class="keyword">not</span> <span class="identifier">defined</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">blue</span><span class="special">)</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">blue</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">(</span><span class="number">1</span><span class="special">))</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">red</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">(</span><span class="number">2</span><span class="special">))</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">green</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">(</span><span class="number">3</span><span class="special">))</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">(</span><span class="number">3</span><span class="special">)</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="identifier">color</span><span class="special">(</span><span class="number">4</span><span class="special">))</span>
<span class="identifier">enums</span><span class="special">.</span><span class="identifier">color</span><span class="special">.</span><span class="identifier">blue</span>

<span class="special">&gt;&gt;&gt;</span> <span class="identifier">identity</span><span class="special">(</span><span class="number">1</span><span class="special">)</span>
<span class="identifier">Traceback</span> <span class="special">(</span><span class="identifier">most</span> <span class="identifier">recent</span> <span class="identifier">call</span> <span class="identifier">last</span><span class="special">):</span>
  <span class="identifier">File</span> <span class="string">"&lt;stdin&gt;"</span><span class="special">,</span> <span class="identifier">line</span> <span class="number">1</span><span class="special">,</span> <span class="identifier">in</span> <span class="special">?</span>
<span class="identifier">TypeError</span><span class="special">:</span> <span class="identifier">bad</span> <span class="identifier">argument</span> <span class="identifier">type</span> <span class="keyword">for</span> <span class="identifier">built</span><span class="special">-</span><span class="identifier">in</span> <span class="identifier">operation</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_docstring_options_h.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_errors_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
